<?php 
$req_path="../";
include($req_path."includes/ini.php");
include($req_path."includes/db.php");

class M_Leave extends DB{
    private $rowCount=0;
    public $table_name="tb_leave";
	
 
	
    function M_Leave(){
		parent::__construct();
    }
    function loadData($type=0,$name='', $df='', $dt=''){
        
        $sqlscript= " SELECT  le.id, d.name, s.name as typename, le.reason, DATE_FORMAT(le.date_from,'%d/%m/%Y') datefrom ,  DATE_FORMAT(le.date_to,'%d/%m/%Y') dateto";
		$sqlscript.= " FROM tb_leave le ";
		$sqlscript.= " LEFT JOIN tb_employee d on d.id = le.eid ";
		$sqlscript.= " LEFT JOIN tb_leave_type s on s.id = le.type ";
		$sqlscript.= " WHERE 1=1 ";
		if ($name !=''){
			$sqlscript.= " and d.name like '".$name."'" ;
		}
		if ($type != 0){
			$sqlscript.= " and le.type = ".$type ;
		}
		if ($df != ''){
			$sqlscript.= " and le.date_from >= '".$df."' and le.date_to <= '".$dt."'" ;
		}
		 
    
        $result = $this->ExecuteData($sqlscript);
       $this->rowCount = $result->RecordCount();
        return $result;
    }
    function getRowCount(){
        
        return $this->rowCount;
        
    }	
	function getEmp(){
		$sql = " SELECT id
			 ,  CONCAT( case when em.title='0' then 'นาย' when em.title='1' then 'นางสาว' else 'นาง' end   ,' ', name) as name
			  FROM tb_employee em WHERE 1=1  order by name asc ";
			  
		$result = $this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["id"]] = $row["name"];
		}
		return $ar;	
	}
	function getLeave($type=''){
	 
		$sqlscript= "select id, name from tb_leave_type where 1=1 ";
		if($type!=''){
			$sqlscript .= " and status = '".$type."' ";
		}
		$sqlscript .=" Order by id asc ";
		
		$result = $this->ExecuteData($sqlscript);
		while($row = $result->FetchRow()){
			$ar[$row["name"]] = $row["id"];
		}
		return $ar;	
	}
	 
	
    function insertData($val){
            if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT  date_from, date_to, type, eid, reason ,createdate,createby FROM ". $this->table_name." where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
    }
    function updateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT  id, date_from, date_to, type, eid, reason, updatedate,updateby  FROM ". $this->table_name." where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
    }
    function deleData($id){
            if(isset($id)){
			$sqlDelete = "Delete FROM ". $this->table_name." where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			return 1;
            }
    }
    
}
?>
